package gui.jtab;

import event.notifyTexture.NotifyTextureListenerInterface;
import gui.GLJPanels.GLJPanelNW;
import gui.GLListeners.GLListener;
import gui.jtab.common.JTabTextureOptions;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observer;
import java.util.Properties;

import javax.media.opengl.GLException;
import javax.swing.JPanel;

import utils.files.FileManager;
import utils.images.ProcesadorImagenes;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import constantes.Consts;

public abstract class JTab extends JPanel implements JTabInterface, Observer, NotifyTextureListenerInterface{

	protected GLListener GLL;
	protected GLListener GLLVisor1;
	protected GLListener GLLVisor2;
	protected GLListener GLLVisor3;	

	protected GLJPanelNW visor1;
	protected GLJPanelNW visor2;
	protected GLJPanelNW visor3;
	protected GLJPanelNW canvas;
	protected JTabTextureOptions texturePanel;
	
	protected boolean enabledMenuGuardar = false;
	
	
	@Override
	public void guardarComo(File file) {
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
			canvas.getContext().makeCurrent();
			
			String name = Consts.FOLDER_TMP+format.format(new Date());
			File tmp = new File(name);
			tmp.mkdirs();
			
			Dimension size = canvas.getSize();
			BufferedImage myImage = new BufferedImage(size.width, size.height,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g2 = myImage.createGraphics();
			canvas.paint(g2);
			
			OutputStream out = new FileOutputStream(name+".jpg");
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			encoder.encode(myImage);
			out.close();
			
			ProcesadorImagenes pro = new ProcesadorImagenes();
			
			BufferedImage image = pro.escalarATamanyo(new File(name+".jpg"), Consts.IMG_MINI_SIZE,Consts.IMG_MINI_SIZE, "jpg");
				pro.salvarImagen(image, name+"/"
						+ file.getName()+".mini", "jpg");
			
			//guardamos los puntos del objeto.
			GLL.save(tmp.getAbsolutePath()+"/"+file.getName());
			
			//comprimimos los componentes
			FileManager.zipComponent(tmp.getAbsolutePath(), file.getAbsolutePath());
			
			canvas.repaint();
			canvas.requestFocus();
			enabledMenuGuardar = true;
		} catch (GLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ImageFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void cargarTextura(String path){
		File directory = new File(path);
		String name = directory.getName();
		directory = directory.getParentFile();
		File[] files = directory.listFiles();
		for(File f: files){
			if(f.getName().length()!= name.length() && !f.getName().endsWith(".mini")&& !f.getName().endsWith(".properties")){
				GLL.setTexture(f.getAbsolutePath());
				texturePanel.setImage(f.getAbsolutePath());
			}
			if(f.getName().endsWith(".properties")){
				try {
					Properties properties = new Properties();
					InputStreamReader input = new InputStreamReader(new FileInputStream(f));
					properties.load(input);
					GLL.setTextureBlended(new Boolean(properties.getProperty("texture.blended")));
					texturePanel.setTextureBlended(new Boolean(properties.getProperty("texture.blended")));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}	
		}		
	}

	public boolean isEnabledMenuGuardar() {
		return enabledMenuGuardar;
	}

	
}
